#include "input.h"

static FILE *myfile;
static int retval;

void START(){
	EndOfFile = false;
	myfile = fopen(Namafile,"r");
	retval = fscanf(myfile,"%c",&CC);
	if(retval == -1){
		EndOfFile = true;
		printf("File tidak ditemukan\n");
	}
}

void ADV(){
	retval = fscanf(myfile,"%c",&CC);
	if(retval == -1){
		EndOfFile = true;
	}
}

void IgnoreBlank(){
	while(((CC == ' ') || (CC == '\n') || (CC == '\t')) && (EndOfFile == false)){
		ADV();
	}
}

void STARTLINE(){
	START();
	IgnoreBlank();
	lidx = 0;
	lnum = 0;
	int i = 0;
	while((CC!='\n') && (EndOfFile == false)){
		CS[i]=CC;
		i++;
		ADV();
	}
	CS[i]='\0';
	strcpy(Line[lidx],CS);
	lidx ++;
	lnum ++;
}

void ADVLINE(){
	IgnoreBlank();
	int i = 0;
	while((CC != '\n') && (EndOfFile == false)) {
		CS[i]=CC;
		i++;
		ADV();
	}
	CS[i]='\0';
	strcpy(Line[lidx],CS);
	lidx ++;
	lnum ++;
}

void CloseFile(){
	fclose(myfile);
}

void Read(char* filename){
	strcpy(Namafile,filename);
    memset(Line,0,sizeof(Line));
    lnum = 0;
    lidx = 0;
    
	STARTLINE();
	while (EndOfFile != true){
		ADVLINE();
	}
	CloseFile();
}

void OutputStart(){
	myfile = fopen("tes1.out","w");
	WriteLine("File input: ");
	WriteLine(Namafile);
	WriteLine("\r\n");
}

void WriteLine(char* args){
	fprintf(myfile,"%s",args);
}
void WriteInt(int args){
	fprintf(myfile,"%d",args);
}